package com.freedom.auth.controller;

import com.freedom.auth.dto.RoleDTO;
import com.freedom.auth.service.IRoleService;
import com.freedom.auth.domain.Role;
import com.freedom.auth.query.RoleQuery;
import com.freedom.base.util.AjaxResult;
import com.freedom.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     *
     * @param role 传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult insertOrUpdate ( @RequestBody Role role ) {
        try {
            if (role.getId() != null) {
                roleService.update(role);
                return AjaxResult.success("修改成功");
            } else {
                roleService.insert(role);
                return AjaxResult.success("新增成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete ( @PathVariable Serializable id ) {
        try {
            roleService.deleteById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult getById ( @PathVariable("id") Serializable id ) {
        try {
            Role role = roleService.getById(id);
            return AjaxResult.success(role, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的角色信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult getAll () {
        try {
            List<Role> list = roleService.getAll();
            return AjaxResult.success(list, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PutMapping
    public AjaxResult queryPage ( @RequestBody RoleQuery query ) {
        try {
            PageInfo<Role> pageInfo = roleService.queryPage(query);
            return AjaxResult.success(pageInfo, "分页查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败！" + e.getMessage());
        }
    }


    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDelete ( @RequestBody List<Long> ids ) {
        try {
            roleService.batchDelete(ids);
            return AjaxResult.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败");
        }
    }


    /**
     * 获取角色权限id
     */
    @GetMapping("/permission/{id}")
    public AjaxResult getRolePermissionIds ( @PathVariable("id") Long id ) {
        try {
            List<Long> ids = roleService.getRolePermissionIds(id);
            return AjaxResult.success(ids, "授权成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("授权失败");
        }
    }

    /**
     *获取角色权限
     */
    @PostMapping("/permission")
    public AjaxResult getRoleAuth ( @RequestBody RoleDTO roleDTO ) {
        try {
           roleService.getRoleAuth(roleDTO);
            return AjaxResult.success("授权成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("授权失败");
        }
    }
}
